const User = require("../model/user.model");
class UserService {
  /**
   * 参加用户数据
   * @param {*} param0
   * @returns
   */
  async createUser({ username, password }) {
    const res = await User.create({
      username,
      password,
    });
    return res ? res.dataValues : null;
  }

  /**
   * 获取用户信息
   * @param {*} param0
   * @returns
   */
  async getUserInfo({ id, username, password, isAdmin }) {
    const whereOpt = {};
    id && Object.assign(whereOpt, { id });
    username && Object.assign(whereOpt, { username });
    password && Object.assign(whereOpt, { password });
    isAdmin && Object.assign(whereOpt, { isAdmin });

    const res = await User.findOne({
      attributes: ["id", "username", "password", "isAdmin"],
      where: whereOpt,
    });
    return res ? res.dataValues : null;
  }
  /**
   * 根据用户id 修改用户信息
   * @param {*} param0 
   * @returns 
   */
  async updateById({ id, username, password, isAdmin }){
    const whereOpt = {id};
    const newUser = {}
    username && Object.assign(newUser, { username });
    password && Object.assign(newUser, { password });
    isAdmin && Object.assign(newUser, { isAdmin });
    const res = await User.update(newUser, { where: whereOpt})
    return res[0] > 0
  }
}

module.exports = new UserService();
